package com.cema.manage.project.manage.friend.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cema.manage.project.android.model.PageVo;
import com.cema.manage.project.android.model.result.BizFriend;
import com.cema.manage.project.manage.friend.domain.Friend;
import com.cema.manage.project.manage.friend.mapper.FriendMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 邀请好友 服务层实现
 *
 * @author reasahi
 * @date 2024-07-08
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {
    @Resource
    private FriendMapper friendMapper;

    @Override
    public List<BizFriend> queryBizFriend(Integer usId, Integer effective, PageVo pageVo) {
        Map<String, Object> map = new HashMap<>(3);
        int current = (Integer.parseInt(pageVo.getCurrent()) - 1) * Integer.valueOf(pageVo.getSize());
        map.put(PageVo.CURRENT, current);
        map.put(PageVo.LIMIT, Integer.valueOf(pageVo.getSize()));
        map.put("usId", usId);
        map.put("effective", effective);
        return friendMapper.queryBizFriend(map);
    }

    @Override
    public List<Integer> getUserIdList(Integer plUserId, Integer usId) {
        List<Integer> userIdList = getUserIdList(plUserId);
        if (ObjectUtil.isNull(usId)) {
            userIdList.add(plUserId);
            return userIdList;
        }
        boolean flag = userIdList.stream().anyMatch(usId::equals);
        if (flag || plUserId.equals(usId)) {
            userIdList.clear();
            userIdList.add(usId);
            return userIdList;
        }
        return null;
    }

    //代理运行访问的用户记录
    @Override
    public List<Integer> getUserIdList(Integer fusId) {
        EntityWrapper<Friend> entityWrapper = new EntityWrapper<>();
        Friend friend = new Friend();
        friend.setFusId(fusId);
        entityWrapper.setEntity(friend);
        List<Friend> fieldList = selectList(entityWrapper);
        if (ObjectUtil.isNull(fieldList) || fieldList.size() == 0) {
            return new ArrayList<>();
        }
        return fieldList.stream().map(Friend::getUsId).collect(Collectors.toList());
    }

}
